﻿using System;
using System.Collections.Generic;
using ExFlash.Records;

namespace ExFlash.Tags
{
    public class DefineTextTag : Tag, IHasCharacterId
    {
        public ushort CharacterId
        {
            get;
            protected set;
        }

        public RectangleRecord TextBounds
        {
            get;
            protected set;
        }

        public MatrixRecord TextMatrix
        {
            get;
            protected set;
        }

        public byte GylphBits
        {
            get;
            protected set;
        }

        public byte AdvanceBits
        {
            get;
            protected set;
        }

        public List<TextRecord> TextRecords
        {
            get;
            protected set;
        }

        public DefineTextTag(int len, int offset)
            : this(TagCodes.DefineText, len, offset)
        {

        }

        protected DefineTextTag(TagCodes code, int len, int offset)
            : base(code, len, offset)
            
        {
            
        }

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            CharacterId = reader.ReadUInt16();

            TextBounds = new RectangleRecord();
            TextBounds.SwfDeserialize(reader);

            TextMatrix = new MatrixRecord();
            TextMatrix.SwfDeserialize(reader);

            GylphBits = reader.ReadByte();

            AdvanceBits = reader.ReadByte();

            TextRecords = new List<TextRecord>();

            while (reader.PeekByte() != 0)
            {
                TextRecord rec = new TextRecord(GylphBits, AdvanceBits, TagCode);
                rec.SwfDeserialize(reader);
                TextRecords.Add(rec);
            }

            reader.Position++;
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #region IHasCharacterID Members

        public ushort Id
        {
            get { return CharacterId; }
        }

        #endregion
    }
}
